clear all
use  "../data/zip_year_data", clear

*Define CHTV and CHTC  
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)) 
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)) 

*Since we're only using cross-sectional variation in this regression, define the IV to be just log of the 2000 Chinese population share
gen chi_iv1 = log(chineseper)

*Generate the change in emp and avgincome between 2001 and 2007
gen tmp1 = totalemp if year == 2001
gen tmp2 = totalemp if year == 2007
by zipcode, sort: egen temp1 = max(tmp1)
by zipcode: egen temp2 = max(tmp2)
gen d_lnemp_0107 = log(temp2/temp1)
drop tmp1 tmp2 temp1 temp2

gen tmp1 = avgincome if year == 2001
gen tmp2 = avgincome if year == 2007
by zipcode, sort: egen temp1 = max(tmp1)
by zipcode: egen temp2 = max(tmp2)
gen d_lnincome_0107 = log(temp2/temp1)
drop tmp1 tmp2 temp1 temp2

*Calculate aggregate values of CHTV/CHTC between 2008 and 2013
by zipcode, sort: egen chtv0813 = sum(chtv) if year > 2007
by zipcode, sort: egen chtc0813 = sum(chtc) if year > 2007
gen lnchtv0813 = log(chtv0813)
gen lnchtc0813 = log(chtc0813)

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtv0813 == . 
drop if lnchtc0813 == .

*keep only 1 ob per zip code
keep lnpop bachelorshare ln_density lnchtv0813 lnchtc0813 chi_iv1 d_lnemp_0107 d_lnincome_0107 d_lnemp_9600 d_lnincome_9801 zipcode county
duplicates drop

****Regressions****
*pre-policy emp
ivreghdfe d_lnemp_0107 lnpop bachelorshare ln_density  (lnchtv0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r1
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

ivreghdfe d_lnemp_0107 lnpop bachelorshare ln_density  (lnchtc0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r2
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

*pre-policy income
ivreghdfe d_lnincome_0107 lnpop bachelorshare ln_density  (lnchtv0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r3
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

ivreghdfe d_lnincome_0107 lnpop bachelorshare ln_density  (lnchtc0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r4
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

*pre-period emp
ivreghdfe d_lnemp_9600 lnpop bachelorshare ln_density  (lnchtv0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r5
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

ivreghdfe d_lnemp_9600 lnpop bachelorshare ln_density  (lnchtc0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r6
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

*pre-period income
ivreghdfe d_lnincome_9801 lnpop bachelorshare ln_density  (lnchtv0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r7
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

ivreghdfe d_lnincome_9801 lnpop bachelorshare ln_density  (lnchtc0813 = chi_iv1), absorb(county) cluster(zipcode)
eststo r8
estadd local c "Yes"
estadd local cyfe "Yes"
estadd scalar fstat = e(widstat)

esttab r1 r2 r3 r4 r5 r6 r7 r8 ///
	using "../results/table5.tex", replace ///
	label ///
	keep(lnchtv0813 lnchtc0813) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(c cyfe fstat N, ///
		fmt(0 0 %3.0f %11.0gc) ///
		labels( "Controls" ///
			"County FE" ///
			"First Stage F-statistic" ///
			"Observations")) ///
	mlabels(, none) collabels(, none) ///
	mgroups("Pre-Policy Employment" "Pre-Policy Income" "Pre-Period Employment" "Pre-Period Income", pattern(1 0 1 0 1 0 1 0))
